package com.westcom.vulb.logic;

import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.json.JSONException;
import org.json.JSONObject;

import com.westcom.vulb.network.http.VulbHttp;
import com.westcom.vulb.protocal.http.Response;

public class category extends BasicLogic 
{
	private static String CreateCategory_url = base_url+"web/create_sheet";
	private static String GetCategoryList_url = base_url+"web/category_list";
	private static String UpdateCategory_url = base_url+"web/edit_category";
	private static String DeleteCategory_url = base_url+"web/delete_category";
	private static String MoveCategory_url = base_url+"web/move_category";
	private static String GetCategoryInfo_url = base_url+"web/category_statistics";
	
	public static category getInstanse()
	{
		return new category();
	}
	
	public Response CreateCategory(String sessionId,int sheetId,String categoryName,int dispCol,int colorIdx)
	{
	    JSONObject j = new JSONObject();
		try 
		{
			j.put("sheetId", sheetId);
			j.put("categoryName", categoryName);
		    j.put("dispCol", dispCol == 0? "categorized_0":"categorized_1");
		    j.put("colorIdx", colorIdx);
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
		builder.addTextBody("params", j.toString());
		builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
		Response response = VulbHttp.post(CreateCategory_url,buildHeader(sessionId),builder.build());
		return response;
	}
	
	
	public Response GetCategoryList(String sessionId,int sheetId)
	{
		JSONObject j = new JSONObject();
		try 
		{
			j.put("sheetId", sheetId);
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
		builder.addTextBody("params", j.toString());
		builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
		Response response = VulbHttp.post(GetCategoryList_url,buildHeader(sessionId),builder.build());
		return response;		  
	}
	
	public Response UpdateCategory(String sessionId,int categoryId,String categoryName,int colorIdx)
	{
		JSONObject j = new JSONObject();
		try 
		{
			j.put("categoryId", categoryId);
			j.put("categoryName", categoryName);
			j.put("colorIdx", colorIdx);
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
		builder.addTextBody("params", j.toString());
		builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
		Response response = VulbHttp.post(UpdateCategory_url,buildHeader(sessionId),builder.build());
		return response;
	}
	
	public Response DeleteCategory(String sessionId,int[]categoryIds)
	{
		JSONObject j = new JSONObject();
		try 
		{
			j.put("categoryIds", intArray2JSONArray(categoryIds));
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
		builder.addTextBody("params", j.toString());
		builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
		Response response = VulbHttp.post(DeleteCategory_url,buildHeader(sessionId),builder.build());
		return response;
	}
	public Response MoveCategory(String sessionId,int[] categoryIds,int sheetId,int targetDispCol,int targetCategoryId)
	{
		JSONObject j = new JSONObject();
		try 
		{
			j.put("categoryIds", intArray2JSONArray(categoryIds));
			j.put("sheetId", sheetId);
			j.put("targetDispCol", targetDispCol==0?"categorized_0":"categorized_1");
			j.put("targetCategoryId", targetCategoryId);
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
		builder.addTextBody("params", j.toString());
		builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
		Response response = VulbHttp.post(MoveCategory_url,buildHeader(sessionId),builder.build());
		return response;
	}
	public Response GetCategoryInfo(String sessionId,int[] categoryIds)
	{
		JSONObject j = new JSONObject();
		try 
		{
			j.put("categoryIds", intArray2JSONArray(categoryIds));
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
		builder.addTextBody("params", j.toString());
		builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
		Response response = VulbHttp.post(GetCategoryInfo_url,buildHeader(sessionId),builder.build());
		return response;
	}
}
